package Data.Operation;

import java.io.File;
import java.io.*;
import java.util.ArrayList;

public class FileOperation {
	private File file;
	
	public FileOperation(String s){
		file=new File(s);
	}
	
	public ArrayList readFile(){
		ArrayList ojs=new ArrayList();
			try {
				ObjectInputStream input=new ObjectInputStream(new FileInputStream(file));
				Object temp=input.readObject();
				while(temp!=null) {
					ojs.add(temp);
					temp=input.readObject();
				}
			input.close();
			} 
			catch (FileNotFoundException e) {e.printStackTrace();} 
			catch (IOException e) {}
			catch (ClassNotFoundException e) {}
			return ojs;		
	}
	
	public void newwritefile(ArrayList ojs) throws  IOException{
		ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream(file));
		for(int i=0;i<ojs.size();i++){
			output.writeObject(ojs.get(i));
		}
	}
	
	public void addfile(Object oj) throws  IOException{
		ArrayList ojs=readFile();
		ojs.add(oj);
		newwritefile(ojs);
	}
	
	public void newfile() throws IOException{
		ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream(file));
		output.writeObject(null);
	}

}
